package cn.tongji.edu.tjmobile;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.WriterException;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.util.Calendar;

import cn.tongji.edu.tjmobile.util.QRCodeGenerator;

public class QRCodeReserveCancelActivity extends Activity {

    private Context context;

    private String startCampus;
    private String endCampus;
    private String time;
    private String busId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qrcode_reserve_cancel);

        context = QRCodeReserveCancelActivity.this;

        Intent intent = getIntent();
        startCampus = intent.getStringExtra("start");
        endCampus = intent.getStringExtra("end");
        time = intent.getStringExtra("time");
        busId = intent.getStringExtra("busId");
        final String userId = MainActivity.userId;

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, 1);
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DATE);
        int weekOfDay = cal.get(Calendar.DAY_OF_WEEK);
        String weekOfDayString = "";
        switch (weekOfDay) {
            case 1:
                weekOfDayString = "日";
                break;
            case 2:
                weekOfDayString = "一";
                break;
            case 3:
                weekOfDayString = "二";
                break;
            case 4:
                weekOfDayString = "三";
                break;
            case 5:
                weekOfDayString = "四";
                break;
            case 6:
                weekOfDayString = "五";
                break;
            case 7:
                weekOfDayString = "六";
                break;
        }
        String currentDate = month + "月" + day + "日";
        String week = "周" + weekOfDayString;

        TextView startCampusTextView = (TextView) findViewById(R.id.start_campus_textview);
        startCampusTextView.setText(startCampus);
        TextView endCampusTextView = (TextView) findViewById(R.id.end_campus_textview);
        endCampusTextView.setText(endCampus);

        TextView currentDateTextView = (TextView) findViewById(R.id.current_date_textview);
        currentDateTextView.setText(currentDate);
        TextView weekTextView = (TextView) findViewById(R.id.week_textview);
        weekTextView.setText(week);
        TextView timeLineTextView = (TextView) findViewById(R.id.time_line_textview);
        timeLineTextView.setText(time);

        ImageView qrCodeImageView = (ImageView) findViewById(R.id.qrcode_image_view);

        try {
            Bitmap qrCodeBitmap = QRCodeGenerator.createQRCode(userId + " " + startCampus + "-" + endCampus + " " + currentDate + " " + week + " " + time, 198);
            qrCodeImageView.setImageBitmap(qrCodeBitmap);
        } catch (WriterException e) {
            e.printStackTrace();
        }

        TextView reserveCancelTextView = (TextView) findViewById(R.id.reserve_cancel_textview);
        reserveCancelTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CancelTicketTask task = new CancelTicketTask();
                task.execute(busId);
            }
        });
    }

    class CancelTicketTask extends AsyncTask<String, Integer, String> {

        @Override
        protected String doInBackground(String... params) {
            HttpClient client = new DefaultHttpClient();
            String url = "http://jiading.tongji.edu.cn:8080/TJbus/CancelTicketServlet?bus_id=" + params[0];
            HttpGet request = new HttpGet(url);
            try {
                HttpResponse response = client.execute(request);
                String result = IOUtils.toString(response.getEntity().getContent());
                System.out.print(result);
                Message message = new Message();
                message.obj = result.trim();
                toastHandler.sendMessage(message);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    Handler toastHandler = new Handler() {
        public void handleMessage(Message message) {
            String result = (String) message.obj;
            switch(result){
                case "0":
                    Toast.makeText(context, "退票失败",
                            Toast.LENGTH_SHORT).show();
                    break;
                case "1":
                    Toast.makeText(context, "退票成功",
                            Toast.LENGTH_SHORT).show();
                    break;
                case "2":
                    Toast.makeText(context, "不是退票时间",
                            Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
}
